package com.huawei.codearts.tools.exception;

import com.huawei.codearts.tools.utils.ResponseUtils;
import com.huawei.codearts.tools.utils.ResultResponse;
import com.huawei.codearts.tools.utils.ResultUtils;
import com.huawei.codearts.tools.validated.StatusEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理 HTTP 404 错误
     */
    @ExceptionHandler({NoResourceFoundException.class, NoHandlerFoundException.class})
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public Object handleNotFound(HttpServletRequest req, Exception ex) {
        System.out.println("handleNotFound..." + HttpStatus.NOT_FOUND.value() + " NOT_FOUND: " + req.getRequestURI());
//        return ResponseUtils.error(HttpStatus.NOT_FOUND.value() + " NOT_FOUND: " + req.getRequestURI());
        return ResultUtils.error(StatusEnum.NOT_FOUND);
    }

    /**
     * 处理 HTTP 500 错误
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handleInternalServerError(HttpServletRequest req, Exception ex) {
        System.out.println("handleInternalServerError..." + HttpStatus.INTERNAL_SERVER_ERROR.value() + "服务器内部错误: " + ex.getMessage());
//        return ResponseUtils.logException(ex);
        return ResultUtils.exception(ex);
    }

    /**
     * 处理接口运行时的异常
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handleRuntimeExceptions(HttpServletRequest req, RuntimeException ex) {
        System.out.println("handleRuntimeExceptions..." + HttpStatus.INTERNAL_SERVER_ERROR.value() + "运行错误: " + ex.getMessage());
        return ResultUtils.runtimeException(ex);
    }

    /**
     * 处理其他所有未处理的异常
     */
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handleOtherExceptions(HttpServletRequest req, Throwable ex) {
        System.out.println("handleOtherExceptions..." + HttpStatus.INTERNAL_SERVER_ERROR.value() + "系统内部错误: " + ex.getMessage());
        return ResultUtils.exception(ex);
    }

    /**
     * 处理自定义 ServiceException 异常
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ResultResponse<ServiceException> handleServiceException(HttpServletRequest req, ServiceException ex) {
        System.out.println("handleServiceException...code: " + ex.getStatus().code + ", msg: " + ex.getMessage());
        return ResultUtils.customException(ex);
    }

    /**
     * 处理自定义 CustomException 异常
     */
    @ExceptionHandler(CustomException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ResultResponse<CustomException> handleCustomException(HttpServletRequest req, CustomException ex) {
        System.out.println("handleCustomException...code: " + ex.getCode() + ", msg: " + ex.getMessage());
        return ResultUtils.customException(ex);
    }

}

